import { routerRedux } from 'dva/router';
import { setToken, getToken } from '@/utils/authority';
import { getPageQuery, getAuthorityFromRouter } from '@/utils/utils';

const Model = {
  namespace: 'token',
  state: {
    token: undefined,
  },
  effects: {
    *save({ payload }, { put }) {
      yield put({
        type: 'changeToken',
        payload,
      });
      yield setToken(payload.token);
      if (payload.callback) {
        payload.callback();
      }
    },

    *remove({ payload }, { put }) {
      // todo 调用login out 接口
      // 删除token
      yield put({
        type: 'changeToken',
        payload: { token: undefined },
      });
      yield setToken(undefined);

      if (payload && payload.callback) {
        payload.callback();
      }
      // 跳转到登录界面
      // location.href=""
    },
  },
  reducers: {
    changeToken(state, { payload }) {
      return { token: payload.token };
    },
  },
};
export default Model;
